//
//  SDHomeProductList.m
//
//  Created by   on 2018/11/15
//  Copyright (c) 2018 __MyCompanyName__. All rights reserved.
//

#import "SDHomeProductList.h"


NSString *const kSDHomeProductListSketch = @"sketch";
NSString *const kSDHomeProductListImg = @"img";
NSString *const kSDHomeProductListId = @"id";
NSString *const kSDHomeProductListProductName = @"productName";
NSString *const kSDHomeProductListSImg = @"sImg";
NSString *const kSDHomeProductListDescription = @"description";
NSString *const kSDHomeProductListBImg = @"bImg";


@interface SDHomeProductList ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SDHomeProductList

@synthesize sketch = _sketch;
@synthesize img = _img;
@synthesize id = _id;
@synthesize productName = _productName;
@synthesize sImg = _sImg;
@synthesize description = _description;
@synthesize bImg = _bImg;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.sketch = [self objectOrNilForKey:kSDHomeProductListSketch fromDictionary:dict];
            self.img = [self objectOrNilForKey:kSDHomeProductListImg fromDictionary:dict];
            self.id = [[self objectOrNilForKey:kSDHomeProductListId fromDictionary:dict] doubleValue];
            self.productName = [self objectOrNilForKey:kSDHomeProductListProductName fromDictionary:dict];
            self.sImg = [self objectOrNilForKey:kSDHomeProductListSImg fromDictionary:dict];
            self.description = [self objectOrNilForKey:kSDHomeProductListDescription fromDictionary:dict];
            self.bImg = [self objectOrNilForKey:kSDHomeProductListBImg fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.sketch forKey:kSDHomeProductListSketch];
    [mutableDict setValue:self.img forKey:kSDHomeProductListImg];
    [mutableDict setValue:[NSNumber numberWithDouble:self.id] forKey:kSDHomeProductListId];
    [mutableDict setValue:self.productName forKey:kSDHomeProductListProductName];
    [mutableDict setValue:self.sImg forKey:kSDHomeProductListSImg];
    [mutableDict setValue:self.description forKey:kSDHomeProductListDescription];
    [mutableDict setValue:self.bImg forKey:kSDHomeProductListBImg];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.sketch = [aDecoder decodeObjectForKey:kSDHomeProductListSketch];
    self.img = [aDecoder decodeObjectForKey:kSDHomeProductListImg];
    self.id = [aDecoder decodeDoubleForKey:kSDHomeProductListId];
    self.productName = [aDecoder decodeObjectForKey:kSDHomeProductListProductName];
    self.sImg = [aDecoder decodeObjectForKey:kSDHomeProductListSImg];
    self.description = [aDecoder decodeObjectForKey:kSDHomeProductListDescription];
    self.bImg = [aDecoder decodeObjectForKey:kSDHomeProductListBImg];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_sketch forKey:kSDHomeProductListSketch];
    [aCoder encodeObject:_img forKey:kSDHomeProductListImg];
    [aCoder encodeDouble:_id forKey:kSDHomeProductListId];
    [aCoder encodeObject:_productName forKey:kSDHomeProductListProductName];
    [aCoder encodeObject:_sImg forKey:kSDHomeProductListSImg];
    [aCoder encodeObject:_description forKey:kSDHomeProductListDescription];
    [aCoder encodeObject:_bImg forKey:kSDHomeProductListBImg];
}

- (id)copyWithZone:(NSZone *)zone {
    SDHomeProductList *copy = [[SDHomeProductList alloc] init];
    
    
    
    if (copy) {

        copy.sketch = [self.sketch copyWithZone:zone];
        copy.img = [self.img copyWithZone:zone];
        copy.id = self.id;
        copy.productName = [self.productName copyWithZone:zone];
        copy.sImg = [self.sImg copyWithZone:zone];
        copy.description = [self.description copyWithZone:zone];
        copy.bImg = [self.bImg copyWithZone:zone];
    }
    
    return copy;
}


@end
